﻿// This code is released under the BSD license.
namespace PugLib.UI.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;

    public partial class CustomMessageBox : PugForm
    {
        private readonly string _caption;

        private CustomMessageBox()
        {
            InitializeComponent();
        }

        private CustomMessageBox(string message, string caption, Image image, IEnumerable<string> buttons)
            : this()
        {
            pbIcon.Image = image;
            lblMessage.Text = message;
            _caption = caption;

            AddButtons(buttons);
        }

        private CustomMessageBox(string message, string caption, Image image, IEnumerable<Tuple<string, string>> buttons)
            : this()
        {
            pbIcon.Image = image;
            lblMessage.Text = message;
            _caption = caption;

            AddButtons(buttons);
        }


        public string Result { get; set; }

        public static string Show(string message, string caption, Image image, IEnumerable<string> buttons)
        {
            return CreateDialog(message, caption, image, buttons);
        }

        public static string Show(string message, string caption, Image image, IEnumerable<Tuple<string, string>> buttons)
        {
            return CreateDialog(message, caption, image, buttons);
        }

        private static string CreateDialog(string message, string caption, Image image, IEnumerable<Tuple<string, string>> buttons)
        {
            string result;
            using (CustomMessageBox dialog = new CustomMessageBox(message, caption, image, buttons))
            {
                dialog.ShowDialog();
                result = dialog.Result;
            }
            return result;
        }

        private static string CreateDialog(string message, string caption, Image image, IEnumerable<string> buttons)
        {
            string result;
            using (CustomMessageBox dialog = new CustomMessageBox(message, caption, image, buttons))
            {
                dialog.ShowDialog();
                result = dialog.Result;
            }
            return result;
        }

        private void AddButtons(IEnumerable<string> newButtons)
        {
            List<CustomMessageBoxButton> buttonsToAdd = new List<CustomMessageBoxButton>();
            foreach (string buttonText in newButtons)
            {
                buttonsToAdd.Add(new CustomMessageBoxButton(null, buttonText, string.Empty));
            }

            LayoutButtons(buttonsToAdd);
        }

        private void AddButtons(IEnumerable<Tuple<string, string>> newButtons)
        {
            List<CustomMessageBoxButton> buttonsToAdd = new List<CustomMessageBoxButton>();
            foreach (Tuple<string, string> buttonTexts in newButtons)
            {
                buttonsToAdd.Add(new CustomMessageBoxButton(null, buttonTexts.Item1, buttonTexts.Item2));
            }

            LayoutButtons(buttonsToAdd);
        }

        private void LayoutButtons(List<CustomMessageBoxButton> buttonsToAdd)
        {
            buttonsToAdd.ForEach(b => b.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
            int offset = 8;
            for (int i = 0; i < buttonsToAdd.Count; i++)
            {
                buttonsToAdd[i].Width = pnlButtons.Width - 32;
                buttonsToAdd[i].Location = new Point(16, offset);
                pnlButtons.Controls.Add(buttonsToAdd[i]);
                offset = buttonsToAdd[i].Location.Y + buttonsToAdd[i].Height + 1;
            }
        }

        private void CustomDialogBox_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                Point startCorner = Cursor.Position;
                startCorner.X -= Width / 2;
                Location = startCorner;
                Text = _caption;

                if (pbIcon.Image == null)
                {
                    lblMessage.Dock = DockStyle.Fill;
                }
            }
        }
    }
}